package com.zzx.exception;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

public class CustomExceptionResolver implements HandlerExceptionResolver {

	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		ex.printStackTrace();
		CustomException exception = null;
		if (ex instanceof CustomException) {
			exception = (CustomException) ex;
		}else {
			exception = new CustomException(ex.getMessage());
		}
		String message = exception.getMessage();
		ModelAndView modelAndView = new ModelAndView();
		if (message ==null ||"".equals(message)) {
			message = "未知异常";
		}
		request.setAttribute("message", message);
		try {
			request.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(request, response);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return modelAndView;
	}

}
